package lock.deadlock;

import lombok.SneakyThrows;

import java.util.concurrent.TimeUnit;

/**
 * @author ZhuQi
 * @ClassName DeadLock
 * @Date 2022/9/7 17:46
 * @Version 1.0
 * @Description 死锁案例
 */
public class DeadLock {

    @SneakyThrows
    public static void main(String[] args) {
        Object a = new Object();
        Object b = new Object();
        new Thread(()->{
            String name = Thread.currentThread().getName();
            synchronized (a) {
                System.out.println(name + " ---a");
                try {
                    TimeUnit.MILLISECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (b) {
                    System.out.println(name + " ---b");
                }
            }
        },"thread-a").start();

        new Thread(()->{
            String name = Thread.currentThread().getName();
            synchronized (b) {
                System.out.println(name + " ---b");
                synchronized (a) {
                    System.out.println(name + " ---a");
                }
            }
        },"thread-b").start();
    }
}
